$(function(){
	var navList = $(".navList li"),
		navFixed = $("#nav"),
		line = $("#nav .navList span"),
		line_detail = $("#nav_nf .navList span"),
		navLi = $(".navList li:gt(0):not(.lastLi)"),
		navInner = $(".navinner"),
		pointLi = $(".point li"),
		bannerL = $(".bannerL"),
		bannerUl = $(".bannerUl"),
		oIndex = 0,
		timer = null,
		day = $(".date .day"),
		hour = $(".date .hour"),
		minute = $(".date .minute"),
		sec = $(".date .sec"),
		switchBtn = $(".pu_title .switch a"),
		newSwidth = $(".new_title .switch a"),
		listShow = $(".product .list"),
		sliderImgL = $(".slider .btnL"),
		sliderImgR = $(".slider .btnR"),
		sliderUl = $(".sliderImg ul"),
		showLi1 = $(".showLi1"),
		showLi2 = $(".showLi2"),
		clotheShow = $(".clotheShow"),
		backTopLi = $(".backTop"),
		timer1 = null,
		speed = 0,
		/*登录注册页面js*/
		enterInp = $(".common input"),
		loginInp = $("#main_login input"),
		inpTxt = $("#main_enter .txt"),
		inpTxtL = $("#main_login .txt"),
		inpPsd = $("#main_enter .psd"),
		inpPsdL1 = $("#main_login .psd1"),
		inpPsdL2 = $("#main_login .psd2"),
		inpVc = $("#main_enter .vc_text"),
		inpVcL = $("#main_login .vc1"),
		txtLi = $("#main_enter .txtLi"),
		psdLi = $("#main_enter .psdLi"),
		txtW = $("#main_enter .txtW"),
		psdW1 = $("#main_enter .psdW1"),
		psdW2 = $("#main_enter .psdW2"),
		codeShow = $(".common .code_show"),
		codeReset = $(".common .code_reset"),
		enter = $("#main_enter .login"),
		logIn = $("#main_login .login"),
		vcWar = $("#main_enter .vc_war"),
		checkBox = $("#main_login .cb"),
		registerSub = $("#main_login .register-submit"),
		dayValue = 1,
		hourValue = 23,
		minuteValue = 59,
		secValue = 59;
	
	iTimer();	//页面加载启动定时器
	navList.mouseenter(function(){
		var iIndex = $(this).index();
		line.stop().animate({left:124 * iIndex},150);
		line_detail.stop().animate({left:124 * iIndex},150);

	});
	navList.mouseleave(function(){
		line.stop().animate({left:0},150);
		line_detail.stop().animate({left:0},150);

	});
	navLi.mouseenter(function(){
		var iIndex = $(this).index() - 1;
		navInner.eq(iIndex).css({display:"block"});
	});
	navLi.mouseleave(function(){
		navInner.css({display:"none"});
	});
	pointLi.mouseenter(function(){
		var iIndex = $(this).index();
		oIndex = iIndex;
		pointLi.removeClass("active").eq(iIndex).addClass("active");
		bannerUl.stop().animate({left:iIndex * -955},300);
	});
	function iTimer(){
		timer = setInterval(function(){
			oIndex ++;
			if(oIndex > 5){
				oIndex = 0;
			}
			pointLi.removeClass("active").eq(oIndex).addClass("active");
			bannerUl.stop().animate({left:oIndex * -955},300);
		},3000);
	}
	bannerL.mouseenter(function(){
		clearInterval(timer);
	});
	bannerL.mouseleave(function(){
		iTimer();	//启动定时器
	});
	/**
	 *	定时器控制时间
	*/
	var timer1 = setInterval(function(){
		secValue --;
		if(secValue < 0){
			minuteValue --;
			secValue = 59;
		}
		if(minuteValue == 0){
			hourValue --;
			minuteValue = 59;
		}
		if(hourValue == 0){
			dayValue --;
			hourValue = 11;
		}
		if(dayValue == 0){
			dayValue = 0;
			clearInterval(timer1);
		}
		sec.html(secValue);
		minute.html(minuteValue);
		hour.html(hourValue);
		day.html(dayValue);
	},1000);
	/**
	 *	switch 切换
	*/
	switchBtn.mouseenter(function(){
		var iIndex = $(this).index();
		switchBtn.removeClass("show").eq(iIndex).addClass("show");
		listShow.css({display:"none"}).eq(iIndex).css({display:"block"});
	});
	newSwidth.mouseenter(function(){
		var iIndex = $(this).index();
		newSwidth.removeClass("show").eq(iIndex).addClass("show");
		clotheShow.css({display:"none"}).eq(iIndex).css({display:"block"});
	});
	/**
	 *	slider左右切换
	*/
	sliderImgL.click(function(){
		sliderUl.animate({left:0},function(){
			sliderImgL.css({color:"#ddd"});
			sliderImgR.css({color:"#7d7d7d"});
			showLi1.css({display:"block"});
			showLi2.css({display:"none"});
		});
	});
	sliderImgR.click(function(){
		sliderUl.animate({left:-190},function(){
			sliderImgR.css({color:"#ddd"});
			sliderImgL.css({color:"#7d7d7d"});
			showLi1.css({display:"none"});
			showLi2.css({display:"block"});
		});
	});
	/**
	 *	导航吸顶
	*/
	$(window).scroll(function(){
		speed = ($(window).scrollTop());
		if($(window).scrollTop() >= 150){
			navFixed.addClass("fixed");
			backTopLi.fadeIn();
		}else{
			navFixed.removeClass("fixed");
			backTopLi.fadeOut();
		}
	});
   /**
	 *	返回顶部
	*/
    function backTopFun(){
        speed -= 18;
        ($(window).scrollTop(speed));
        if(($(window).scrollTop()) == 0){
            clearInterval(timer1);
        }
    }
    backTopLi.click(function () {
        timer1 = setInterval(backTopFun,2);
    });
    /**
	 *	登录页面
	 *	获取和失去焦点
	*/
    var regPhone = /^1[34578]\d{9}$/;
    enterInp.focus(function() {
    	$(this).css({background:"#f9f9f9",color:"#000"});
    });
    enterInp.blur(function() {
    	$(this).css({background:"#FFF",color:"#666"});
    });
    /**
	 *	注册页面
	 *	获取和失去焦点
	*/
    loginInp.focus(function(){
    	$(this).css({border:"1px solid #fd9819"});
    	$(this).next().css({display:"block"});

    });
    loginInp.blur(function(){
    	$(this).css({border:"1px solid #CCC"});
    	$(this).next().css({display:"none"});
    });
    /**
	 *	手机号/邮箱/登录
	*/
	inpTxt.blur(function(){
		if(! regPhone.test(inpTxt.val())){
			txtW.css({display:"block"});
			txtLi.animate({left:-16},100).animate({left:16},100).animate({left:-8},100).animate({left:0},100,function(){
				txtW.fadeOut(1500);
			})
    	}
    });
    /**
	 *	密码
	*/
    inpPsd.blur(function(){
    	if(inpPsd.val() == ""){
    		psdW1.css({display:"block"});
    		psdLi.animate({left:-16},100).animate({left:16},100).animate({left:-8},100).animate({left:0},100,function(){
    			psdW1.fadeOut(1500);
    			});
    	}else if(inpPsd.val().length < 6 || inpPsd.val().length >15){
    		psdW2.css({display:"block"});
    		psdLi.animate({left:-16},100).animate({left:16},100).animate({left:-8},100).animate({left:0},100,function(){
    			psdW2.fadeOut(1500);
    			});
    	}

    });
    /**
	 *	注册页面手机号码判断
	*/
 	inpTxtL.blur(function(){phoneNum();});//调用函数
    function phoneNum(){
    	if(inpTxtL.val() == ""){
    		inpTxtL.next().addClass("error_msg").css({display:"block"});
    		inpTxtL.next().html("手机号码不能为空");
    		setTimeout(removeErr,2000);
    		function removeErr(){
    			inpTxtL.next().removeClass("error_msg").html("完成验证后，您可以用该手机号登录和找回密码").css({display:"none"});
    		}
    	}
    	if(inpTxtL.val() != "" && ! regPhone.test(inpTxtL.val())){
    		inpTxtL.next().addClass("error_msg").css({display:"block"});
    		inpTxtL.next().html("手机号格式有误，请输入正确手机号");
    		setTimeout(removeErr,2000);
    		function removeErr(){
    			inpTxtL.next().removeClass("error_msg").html("完成验证后，您可以用该手机号登录和找回密码").css({display:"none"});
	    	}
	    }
    };
    /**
	 *	注册页面验证码判断
	*/
    inpVcL.blur(function(){codeCheck();});
    function codeCheck(){
		if(inpVcL.val() == ""){
			inpVcL.next().addClass("error_msg").css({display:"block"});
    		inpVcL.next().html("验证码不能为空！");
    		setTimeout(removeErr,2000);
    		function removeErr(){
    			inpVcL.next().removeClass("error_msg").html("输入右边图片数字进行验证").css({display:"none"});
	    	}
		}
	    
    }
    /**
	 *	注册页面密码
	*/
    inpPsdL1.blur(function(){passWordL1();});
    function passWordL1(){
    	if(inpPsdL1.val() == "" || (inpPsdL1.val().length < 6 || inpPsdL1.val().length >15)){
    		inpPsdL1.next().addClass("error_msg").css({display:"block"});
    		inpPsdL1.next().html("密码不能为空且长度在6-15之间");
    		setTimeout(removeErr,2000);
    		function removeErr(){
    			inpPsdL1.next().removeClass("error_msg").html("6-15位字母，建议字母、数字及下划线两种以上组合").css({display:"none"});
	    		}
	    }
	}
	inpPsdL2.blur(function(){passWordL2();});
	function passWordL2(){
    	if(inpPsdL2.val() != inpPsdL1.val()){
    		inpPsdL2.next().addClass("error_msg").css({display:"block"});
    		inpPsdL2.next().html("两次密码输入不一致");
    		setTimeout(removeErr,2000);
    		function removeErr(){
    			inpPsdL2.next().removeClass("error_msg").html("请再次输入密码").css({display:"none"});
    		}
    	}
	}
    /**
	 *	登录验证码
	*/
	fn1();	//页面加载随机显示验证码
    function fn1(){
		var array = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F","G","H",
		"I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
		"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
		var str = new String(4);
		str[0] = array[parseInt(Math.random()*62)];
		str[1] = array[parseInt(Math.random()*62)];
		str[2] = array[parseInt(Math.random()*62)];
		str[3] = array[parseInt(Math.random()*62)];
		codeShow.html(str[0] + str[1] + str[2] + str[3]);
	}
	codeReset.click(function(){
		fn1();	//点击刷新验证码
	});
	
	enter.click(function(){
		if(inpVc.val().toLowerCase() != codeShow.text().toLowerCase()){
			fn1();	//如果验证码错误刷新验证码
			vcWar.css({display:"block"});
			vcWar.fadeOut(1500);
		}else if(inpVc.val().toLowerCase() == codeShow.text().toLowerCase()){
			if(! regPhone.test(inpTxt.val())){	//再次判断文本框
				txtW.css({display:"block"});
    			txtLi.animate({left:-16},100).animate({left:16},100).animate({left:-8},100).animate({left:0},100,function(){
    				txtW.fadeOut(1500);
    			});
	    	}
			else if(inpPsd.val() == ""){	//再次判断密码框
    		psdW1.css({display:"block"});
    		psdLi.animate({left:-16},100).animate({left:16},100).animate({left:-8},100).animate({left:0},100,function(){
    			psdW1.fadeOut(1500);
    			});
	    	}else if(inpPsd.val().length < 6 || inpPsd.val().length >15){
	    		psdW2.css({display:"block"});
	    		psdLi.animate({left:-16},100).animate({left:16},100).animate({left:-8},100).animate({left:0},100,function(){
	    			psdW2.fadeOut(1500);
	    			});
	    	}
		}
	});
	/**
	 *	注册成功
	*/
	logIn.click(function(){
		if(inpVcL.val().toLowerCase() != codeShow.text().toLowerCase()){
			fn1();	//如果验证码错误刷新验证码
			codeCheck();
		}else if(inpTxtL.val() == "" || (inpTxtL.val() != "" && ! regPhone.test(inpTxtL.val()))){
			phoneNum();	//点击注册调用函数进行判断
		}else if(inpPsdL1.val() == "" || (inpPsdL1.val().length < 6 || inpPsdL1.val().length >15)){
			passWordL1();
		}else if(inpPsdL2.val() != inpPsdL1.val()){
			passWordL2();
		}
	});
	if(checkBox.length != 0){
		//判断是否阅读协议
		function JudgeCheckBox(){
			if(checkBox.prop('checked') == false){
				registerSub.attr('disabled','disabled').css({'background':'#fff','color':'#666'});
			}else{
				registerSub.removeAttr('disabled').css({'background':'#fd9819','color':'#FFF'});
			}
		}
		JudgeCheckBox();
		checkBox.click(function() {
			JudgeCheckBox();
		});
	}
	/**
	 *	首页搜索结果显示
	*/
	var searchTxt = $('.search .txt');
	var crossDom = $('#cross_dm');
	var crossDomList = $('#cross_dm ul');
	searchTxt.bind('input', function () {
		$.ajax({
			url:"http://bhb.b5m.com/api/autofill?jsonpCallback=jQuery1910619415833597887_1475135404023&key=" + searchTxt.val() +"&searchSource=korea&_=1475135404032",
			dataType: 'jsonp',
			jsonp: 'jsonpCallback',
			success: function (data, sInfo, xhr) {
				var str = "";
				for(var i=0; i<data.length; i++){
					str += '<li>'+ data[i].term + '<span>'+ '约'+ data[i].total_count +'个结果</span></li>';
					//循环写进li标签和对应的对象的属性值
				}
				crossDomList[0].innerHTML = str;
			}
		});
	});
	searchTxt.focus(function () {
		crossDom.css({display:'block'});
	});
	searchTxt.blur(function () {
		crossDom.css({display:'none'});
	});
	/**
	 *	nav详情品牌图片
	*/
	var brandsTabs = $('.brandsTab');
	(function GetNavBrands(){
		$.ajax({
			url: '../json/nav-brands.json',
			type: 'GET',
			dataType: 'json',
			success: function(data) {
				for(var i in data){
					if(i == 'one'){
						$.each(data.one[0],function(k,v){
							$(brandsTabs[0]).find('a').eq(k-1).html('<img src="'+ v +'"/>')
						});
					} else if(i == 'two'){
						$.each(data.two[0],function(k,v){
							$(brandsTabs[1]).find('a').eq(k-1).html('<img src="'+ v +'"/>')
						});
					}else if(i == 'three'){
						$.each(data.three[0],function(k,v){
							$(brandsTabs[2]).find('a').eq(k-1).html('<img src="'+ v +'"/>')
						});
					}else if(i == 'four'){
						$.each(data.four[0],function(k,v){
							$(brandsTabs[3]).find('a').eq(k-1).html('<img src="'+ v +'"/>')
						});
					}else if(i == 'five'){
						$.each(data.five[0],function(k,v){
							$(brandsTabs[4]).find('a').eq(k-1).html('<img src="'+ v +'"/>')
						});
					}else if(i == 'six'){
						$.each(data.six[0],function(k,v){
							$(brandsTabs[5]).find('a').eq(k-1).html('<img src="'+ v +'"/>')
						});
					}
				}
			}
		});
	})();
	/**
	 *	列表页数据
	*/
	var productList = $('.product_show ul'),
		pageBtn = $('.page div a');
	if(productList.length != 0){
		pageBtn.click(function() {
			var iIndex = $(this).index();
			pageBtn.css({background:'none', color:'#666'}).eq(iIndex).css({background:'#ed145b', color:'#FFF'})
			ListData(iIndex + 2);
			$("html,body").animate({scrollTop:'133px'},'slow','swing')
		});
		ListData(2);
		function ListData(pagenum) {
			$.ajax({
				url: 'http://list.mogujie.com/search?&_version=1&f=baidusem_4uv5iimn1v&sort=pop&_mgjuuid=9f6f3991-0074-48a4-9f7b-28ed5bbfdfd9&width=220&showH=330&height=330&cKey=pc-wall-v1&userId=&action=magic&fcid=51894&ad=0&ptp=1.PqaaOb.0.0.KGWzS&showW=220&ratio=2%3A3&_=1490076546162',
				type: 'GET',
				dataType: 'jsonp',
				data: {
					'page': pagenum
				},
				success: function(data) {
					var productListHtml = template('list', data.result.wall);
					productList.html(productListHtml);
				},
				error: function(err) {
					alert('获取数据失败！');
				}
			})
		};
	}
	/**
	 *	首页数据
	*/
	var topMainUl = $('#main_top ul'),
		popularUl = $('#popular ul'),
		brandsMoreList = $('#popular .brandMore li'),
		allNewList = $('#allNew .list'),
		randomList = $('#random .content_right ul');
	
	(function GetHomeData() {
		$.ajax({
			url: '../json/index.json',
			dataType: 'json',
			type: 'get',
			success: function(data) {
				console.log(data);
				var topMainHtml = template('main-top', data);
				topMainUl.html(topMainHtml);	//限时抢购
				var popularHtmlBeauty = template('popularListBeauty', data);
				var popularHtmlLadies = template('popularListLadies', data);
				popularUl.eq(0).html(popularHtmlBeauty);
				popularUl.eq(1).html(popularHtmlLadies);	//人气推荐
				var brandsMoreHF = template('brandsMoreF', data);
				var brandsMoreHS = template('brandsMoreS', data);
				brandsMoreList.eq(0).html(brandsMoreHF);
				brandsMoreList.eq(1).html(brandsMoreHS);	//人气品牌
				var newGoodsHF = template('newGoodsF', data);
				var newGoodsHS = template('newGoodsS', data);
				allNewList.eq(0).html(newGoodsHF);	
				allNewList.eq(1).html(newGoodsHS);    //新品上架
				var randomListHF = template('randomListF', data);
				var randomListHS = template('randomListS', data);
				randomList.eq(0).html(randomListHF);
				randomList.eq(1).html(randomListHS);
			},
			error: function(err) {
				alert('获取数据失败！');
			}
		});
	})();
	
});
